package com.example.ch02;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    //用于展示输入的问题和回传的答案
    private EditText et_question;
    private TextView tv_answer;
    private Button btn_q;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et_question=(EditText)findViewById(R.id.et_question);
        btn_q=(Button)findViewById(R.id.btn_q);
        tv_answer=(TextView)findViewById(R.id.tv_answer);

        btn_q.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                passDate();
            }
        });


    }
    //传送问题
    public void passDate(){
        //创建一个intent对象，调用startActivityForResult方法
        Intent intent=new Intent(this,AnswerActivity.class);
        intent.putExtra("question",et_question.getText().toString());
        startActivityForResult(intent,1);
    }

    //重写onActivityResult方法进行回传
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(data!=null){
            if(requestCode==1){
                if(resultCode==1){
                    //在答案控件上显示回传得到的答案
                    String answer=data.getStringExtra("answer");
                    tv_answer.setText(answer);
                }
            }
        }
    }
}